package vno.fux.ui
{
	import fl.controls.Label;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import vno.fux.BasePoint;
	import vno.fux.GlobalEventScheduler;
	import vno.fux.GlobalVar;
	import vno.fux.events.MapEvent;
	import vno.fux.res.pool.BitmapAttacher;
	import vno.fux.res.pool.MapResource;
	import vno.fux.res.pool.MinimapResource;
	import vno.fux.res.pool.ResourcePool;
	import vno.sci.event.MinimapEvent;
	import vno.sci.window.Minimap;

	public class MinimapView extends BaseComponentView
	{
		private var _minimapCore:vno.sci.window.Minimap;
		private var _tip:Label;
		public function MinimapView(model_:BaseComponentModel, controller_:BaseComponentController)
		{
			super(model_, controller_);
			this.mouseEnabled = false;
			var map:BitmapAttacher = new BitmapAttacher;
			(ResourcePool.getInstance().mapRes.getRes(11) as MapResource).thumbMap.attach(map);
			var minimap:Sprite = new Sprite;
			minimap.mouseEnabled = false;
			_minimapCore = new vno.sci.window.Minimap(100, map);
			_minimapCore.x = -29;
			_minimapCore.y = 32;
			minimap.addChild(_minimapCore);
			var mapBorder:BitmapAttacher = new BitmapAttacher;
			(ResourcePool.getInstance().uiRes.getRes(2) as MinimapResource).border.attach(mapBorder);
			mapBorder.x = -36;
			minimap.addChild(mapBorder);
			this.x = GlobalVar.stage.stageWidth -200;
			this.content = minimap;
			_tip = new Label;
			_tip.text = "按M键呼出全图";
			_tip.graphics.beginFill(0xFFFFFF);
			_tip.graphics.drawRoundRect(0,0,_tip.width,_tip.height,2);
			_tip.graphics.endFill();
			_tip.alpha = 0.9;
			_minimapCore.addEventListener(MinimapEvent.MINIMAP_CLICKED, needMoveHandler);
			minimap.addEventListener(MouseEvent.MOUSE_OVER, tr);
			minimap.addEventListener(MouseEvent.ROLL_OUT, tr2);
			minimap.addEventListener(MouseEvent.MOUSE_MOVE, tipSet);
		}
		
		protected function tr2(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			GlobalEventScheduler.getInstance().removeEnterFrameListener(tipLabel);
			try
			{
				(this.content as Sprite).removeChild(_tip);
			}
			catch(ae:ArgumentError){}

		}
		
		protected function tr(event:MouseEvent):void
		{
			/*tipLabel*/_tipCounter = 0;
			_tip.x = event.localX;
			_tip.y = event.localY;
			GlobalEventScheduler.getInstance().addEnterFrameListener(tipLabel);
		}
		
		protected function tipSet(event:MouseEvent):void
		{
			_tipCounter = 0;
			_tip.x = event.localX;
			_tip.y = event.localY;
			try
			{
				(this.content as Sprite).removeChild(_tip);
				GlobalEventScheduler.getInstance().addEnterFrameListener(tipLabel);
			}
			catch(ae:ArgumentError){}

		}
		
		private var _tipCounter:int = 0;
		private function tipLabel(event:Event):void
		{
			_tipCounter++;
			if(_tipCounter>60)
			{
				showTip();
				GlobalEventScheduler.getInstance().removeEnterFrameListener(tipLabel);
			}
		}
		
		private function showTip():void
		{
			(this.content as Sprite).addChild(_tip);
		}
		
		protected function needMoveHandler(event:MinimapEvent):void
		{
			dispatchEvent
			(
				new MapEvent
				(
					MapEvent.START_MOVE, 
					{pos:new BasePoint(event.x*10, event.y*10)}
				)
			);
		}
		public function setCenter(pos:BasePoint):void
		{
			_minimapCore.setCenter(pos);
		}
	}
}
